EhCache, Redis এর সাথে Integration

Java Technologies - স্প্রিং জেডিবিসি (Spring JDBC) Spring JDBC এ Caching |
96
96

স্প্রিং জেডিবিসি (Spring JDBC) তে EhCache এবং Redis এর সাথে ইন্টিগ্রেশন একটি কার্যকরী পদ্ধতি হতে পারে, যেহেতু ক্যাশিং ডেটাবেস অ্যাক্সেসের সংখ্যা কমায়, কর্মক্ষমতা বৃদ্ধি করে এবং দ্রুত অ্যাক্সেস নিশ্চিত করে। ক্যাশিং ব্যবহারের মাধ্যমে অ্যাপ্লিকেশন আরও দ্রুত এবং স্কেলেবল হয়, কারণ ক্যাশে হওয়া ডেটা পুনরায় ডেটাবেস থেকে না এনে সরাসরি ক্যাশ থেকে রিট্রিভ করা যায়।

1. EhCache Integration with Spring JDBC

EhCache হল একটি জনপ্রিয়, ওপেন সোর্স ক্যাশিং লাইব্রেরি যা ডেটা মেমোরি-ভিত্তিক স্টোরেজে সংরক্ষণ করে, যা অ্যাপ্লিকেশনের কর্মক্ষমতা বৃদ্ধি করতে সহায়তা করে।

1.1. EhCache সেটআপ

  • pom.xml এ EhCache এবং Spring Cache-এর ডিপেনডেন্সি যুক্ত করুন:
<dependencies>
    <!-- Spring Cache Dependency -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>5.3.20</version>
    </dependency>

    <!-- EhCache Dependency -->
    <dependency>
        <groupId>org.ehcache</groupId>
        <artifactId>ehcache</artifactId>
        <version>3.9.1</version>
    </dependency>
</dependencies>

1.2. EhCache কনফিগারেশন

এখন EhCache কনফিগার করার জন্য স্প্রিং কনফিগারেশন ফাইল ব্যবহার করতে হবে। এখানে একটি উদাহরণ দেওয়া হলো।

EhCache কনফিগারেশন (XML Config)

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/cache
           http://www.springframework.org/schema/cache/spring-cache-3.0.xsd">

    <!-- Enable caching in Spring -->
    <cache:annotation-driven />

    <!-- EhCache Configuration -->
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <constructor-arg ref="ehCache" />
    </bean>

    <bean id="ehCache" class="org.ehcache.core.EhcacheManager">
        <constructor-arg value="classpath:ehcache.xml" />
    </bean>

</beans>

EhCache কনফিগারেশন (ehcache.xml)

<ehcache xmlns="http://www.ehcache.org/v3">
    <cache alias="employeeCache">
        <key-type>java.lang.Integer</key-type>
        <value-type>com.example.Employee</value-type>
        <resources>
            <heap unit="entries" value="1000" />
            <offheap unit="MB" value="10" />
        </resources>
    </cache>
</ehcache>

1.3. ডেটাবেস ক্যাশিং

এখন, আপনি @Cacheable অ্যানোটেশন ব্যবহার করে স্প্রিং জেডিবিসি মেথডে ক্যাশিং প্রয়োগ করতে পারেন।

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

@Component
public class EmployeeDao {

    @Cacheable(value = "employeeCache", key = "#id")
    public Employee getEmployeeById(int id) {
        // Database query for fetching employee by id
        String sql = "SELECT * FROM Employee WHERE id = ?";
        return jdbcTemplate.queryForObject(sql, new Object[]{id}, new BeanPropertyRowMapper<>(Employee.class));
    }
}

ব্যাখ্যা:

  • @Cacheable অ্যানোটেশনটি EmployeeDao ক্লাসে ব্যবহৃত হয়েছে যাতে যখন ডেটাবেসে কোনো কর্মচারীর তথ্য অনুসন্ধান করা হয়, তখন প্রথমবার এটি ডেটাবেস থেকে আসবে এবং পরবর্তীতে ক্যাশ থেকে তা রিটার্ন করা হবে।

2. Redis Integration with Spring JDBC

Redis একটি ইন-মেমরি ডেটাবেস, যা মূলত ডেটা স্টোরেজ, ক্যাশিং, এবং মেসেজ ব্রোকার হিসেবে ব্যবহৃত হয়। Redis স্প্রিং অ্যাপ্লিকেশনে একটি জনপ্রিয় ক্যাশিং সমাধান হিসেবে ব্যবহৃত হয়।

2.1. Redis সেটআপ

  • pom.xml এ Redis এবং Spring Data Redis-এর ডিপেনডেন্সি যুক্ত করুন:
<dependencies>
    <!-- Spring Data Redis Dependency -->
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-redis</artifactId>
        <version>2.5.6</version>
    </dependency>

    <!-- Lettuce Redis Client Dependency -->
    <dependency>
        <groupId>io.lettuce.core</groupId>
        <artifactId>lettuce-core</artifactId>
        <version>6.0.1</version>
    </dependency>
</dependencies>

2.2. Redis কনফিগারেশন

স্প্রিং অ্যাপ্লিকেশন কনফিগারেশনে Redis সেটআপ করতে হবে।

RedisConfig.java:

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.redis.RedisCacheManager;
import org.springframework.cache.redis.RedisCacheConfiguration;
import org.springframework.cache.redis.RedisCache;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.connection.RedisConnectionFactory;

@Configuration
@EnableCaching
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<String, Object> template = new StringRedisTemplate(connectionFactory);
        return template;
    }

    @Bean
    public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
        RedisCacheManager cacheManager = RedisCacheManager.create(connectionFactory);
        return cacheManager;
    }
}

2.3. Redis ক্যাশিং উদাহরণ

এখন, আপনি @Cacheable অ্যানোটেশনটি Redis ক্যাশিং ব্যবহারের জন্য ব্যবহার করতে পারেন।

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

@Component
public class EmployeeDao {

    @Cacheable(value = "employeeCache", key = "#id")
    public Employee getEmployeeById(int id) {
        // Database query for fetching employee by id
        String sql = "SELECT * FROM Employee WHERE id = ?";
        return jdbcTemplate.queryForObject(sql, new Object[]{id}, new BeanPropertyRowMapper<>(Employee.class));
    }
}

ব্যাখ্যা:

  • এই উদাহরণে @Cacheable অ্যানোটেশনটি ব্যবহার করা হয়েছে, যা Redis ক্যাশে employeeCache নামক ক্যাশে ডেটা সংরক্ষণ করবে। এটি ক্যাশে থেকে ডেটা ফিরিয়ে দেয় এবং যদি ডেটা ক্যাশে না থাকে তবে ডেটাবেস থেকে নিয়ে আসে।

উপসংহার:

  • EhCache এবং Redis স্প্রিং জেডিবিসি তে ক্যাশিং সমাধান হিসেবে অত্যন্ত কার্যকরী।
  • EhCache সাধারণত লোকে মেমরি-ভিত্তিক ক্যাশিংয়ের জন্য ব্যবহার করে, যেখানে ক্যাশড ডেটার পরিমাণ কম থাকে।
  • Redis একটি শক্তিশালী, দ্রুত এবং স্কেলেবল ইন-মেমরি ডেটাবেস এবং ক্যাশিং সিস্টেম হিসেবে ব্যবহৃত হয়, যেখানে বেশি পরিমাণ ডেটা ক্যাশ করা যায়।

উপরোক্ত উদাহরণ এবং কনফিগারেশন অনুসরণ করে আপনি স্প্রিং জেডিবিসি অ্যাপ্লিকেশনে EhCache বা Redis সঠিকভাবে ইন্টিগ্রেট করতে পারবেন, যা আপনার অ্যাপ্লিকেশনকে দ্রুত এবং স্কেলেবল করবে।

Content added By
টপ রেটেড অ্যাপ

স্যাট অ্যাকাডেমী অ্যাপ

আমাদের অল-ইন-ওয়ান মোবাইল অ্যাপের মাধ্যমে সীমাহীন শেখার সুযোগ উপভোগ করুন।

ভিডিও
লাইভ ক্লাস
এক্সাম
ডাউনলোড করুন
Promotion